package reader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import objet.Base;
import objet.BasePrincipale;
import objet.Entreprise;
import objet.Scenario;

public class ReaderScenario {

	private String pathLE;
	private String pathLB;
	private String pathBA;
	private Scenario scenario ;
	public String getPathLE() {
		return pathLE;
	}
	public void setPathLE(String pathLE) {
		this.pathLE = pathLE;
	}
	public String getPathLB() {
		return pathLB;
	}
	public void setPathLB(String pathLB) {
		this.pathLB = pathLB;
	}
	public Scenario getScenario() {
		return scenario;
	}
	public void setScenario(Scenario scenario) {
		this.scenario = scenario;
	}
	
	
	public ReaderScenario(String pathListEntre , String pathListBase , String pathBASE) throws IOException
	{
		this.pathLB=pathListBase;
		this.pathLE=pathListEntre;
		this.pathBA=pathBASE;
		scenario = new Scenario();
		File repertoire = new File(pathLB);
		if (repertoire.exists())
		{
			int numligne = 0;
			InputStream ips=new FileInputStream(pathLB); 
			InputStreamReader ipsr=new InputStreamReader(ips);
			BufferedReader br = new BufferedReader(ipsr);
			
			String ligne;
						
			while ((ligne=br.readLine())!=null){
						if (numligne>0)
						{
							System.out.println(ligne);
							if (ligne.length()>0)
							scenario.addPath(ligne);
						}
						numligne++;
						
			}	
			br.close(); 
			
			
			 }
		else 
			System.err.println("Fichier introuvable");
		
		File repertoirebis = new File(pathLE);
		if (repertoirebis.exists())
		{
			int numligne = 0;
			InputStream ips=new FileInputStream(pathLE); 
			InputStreamReader ipsr=new InputStreamReader(ips);
			BufferedReader br = new BufferedReader(ipsr);
			
			String ligne;
						
			while ((ligne=br.readLine())!=null){
						if (numligne>0)
						{
							scenario.getListEntr().add(new Entreprise(scenario.getBasePrincipale().retourneId(ligne),ligne));
						}
						numligne++;
						
			}	
			br.close(); 
			
			
			
			
			
			
			 }
		else 
			System.err.println("Fichier introuvable");
		
		
		System.out.println(scenario.getPathBase().toString());
		for (int i = 0 ; i < scenario.getPathBase().size();i++)
		{
			File repertoirebase = new File(this.pathBA+"/"+scenario.getPathBase().get(i));
			if (repertoirebase.exists())
			{
				int numligne = 0;
				InputStream ips=new FileInputStream(repertoirebase); 
				InputStreamReader ipsr=new InputStreamReader(ips);
				BufferedReader br = new BufferedReader(ipsr);
				
				String ligne;
				Base base = new Base();
				base.setId(i);
				while ((ligne=br.readLine())!=null){
							if (numligne==0)
								base.setCout(Integer.parseInt(ligne));
							if (numligne>1)
							{
								base.AjoutEntreprise(new Entreprise(scenario.getBasePrincipale().retourneId(ligne),ligne));
							}
							numligne++;
							
				}	
				scenario.getListBase().add(base);
				br.close(); 
				 }
			else 
				System.err.println("Fichier introuvable");
			
		}
	}

		
	
}
	

